﻿using System.ServiceModel.Dispatcher;
using System.Web;
using GlimpseWcf.Core;

namespace GlimpseWcf.Client
{
    public class WcfClientMessageInspector : IClientMessageInspector
    {
        private readonly HttpContextBase _context;

        public WcfClientMessageInspector(HttpContextBase context)
        {
            _context = context;
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            var messageHeader = WcfGlimpseResponseMessageHeader.ReadHeader(reply);
            if (messageHeader != null)
                WcfResponseManager.AddWcfResponseData("Test", messageHeader, _context);
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            request.Headers.Add(new CookieMessageHeader(_context));
            return null;
        }
    }
}
